<template>
  <van-icon
    :color="value ? '#ffa500' : ''"
    :name="value ? 'star' : 'star-o'"
    @click="shoucang"
    :loading="loading"
  />
</template>

<script>
import { addshoucang, delshoucang } from '@/api/article'
export default {
  name: 'collectIndex',
  data () {
    return {
      loading: false
    }
  },
  props: {
    value: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async shoucang () {
      this.loading = true
      try {
        if (this.value) {
          // 已收藏，取消收藏
          await delshoucang(this.userId)
          this.$toast.fail('取消收藏')
        } else {
          // 未收藏，添加收藏
          await addshoucang(this.userId)
          this.$toast.success('收藏成功')
        }
        this.$emit('input', !this.value)
      } catch (err) {
        this.$toast('操作失败，请重试')
      }
      this.loading = false
    }
  }
}
</script>

<style>

</style>
